<?php
error_reporting(-1);
ini_set("display_errors", 0);

/**
 * 检查目录
 * @return array
 */
function check_service($dir)
{
    $path = '';
    $stack[] = $dir;
    while ($stack) {
        $thisdir = array_pop($stack);
        if ($dircont = scandir($thisdir)) {
            $i = 0;
            while (isset($dircont[$i])) {
                if ($dircont[$i] !== '.' && $dircont[$i] !== '..') {
                    $current_file = "{$thisdir}/{$dircont[$i]}";
                    if (is_file($current_file)) {
                        $path[] = "{$thisdir}/{$dircont[$i]}";
                    } elseif (is_dir($current_file)) {
                        $stack[] = $current_file;
                    }
                }
                $i++;
            }
        }
    }
    return $path;
}

function lists_service()
{
    $lists = array();
    $check_dir_path = dirname(dirname(__FILE__)) . '/app/Http/Controllers';
    $dir = check_service($check_dir_path);
    foreach ($dir as $key => $val) {
        $filename = str_replace($check_dir_path, '', $val);
        $ext = @end(explode('.', $filename));
        if ($ext == 'php') {
            $fn = fopen($val, 'r');
            $content = str_replace("\n", "######", str_replace('<?php', '', fread($fn, filesize($val))));
            preg_match_all("/<document>(.*?)<\/document>/", $content, $match);
            if(preg_match("/\*(\s*?)class.*?\*/",$content,$matches)){
                $matches = str_replace("class","",$matches);
                $matches = str_replace("######"," ",$matches);
                $matches = str_replace("*","",$matches);
                $matches = str_replace(" ","",$matches);
                $filename .= ' '.$matches[0];
            }

            if(preg_match("/<About>(.*?)<\/About>/",$content,$matches)){
                $filename = end($matches);
            }

            foreach (current($match) as $mkey => $mval) {
                $lists[$filename][] = $mval;
            }
        }
    }
    return $lists;
}


function show_service_lists()
{
    $lists_result = array();
    $lists = lists_service();
    foreach ($lists as $tkey => $tval) {
        foreach ($tval as $key => $val) {
            preg_match('/<name>(.*?)<\/name>/', $val, $name_match);
            if (!empty($name_match)) {
                $lists_result[$tkey][$key]['name'] = end($name_match);
            }

            preg_match('/<code>(.*?)<\/code>/', $val, $name_match);
            if (!empty($name_match)) {
                $lists_result[$tkey][$key]['code'] = end($name_match);
            }

            preg_match('/<author>(.*?)<\/author>/', $val, $author_match);
            if (!empty($author_match)) {
                $lists_result[$tkey][$key]['author'] = end($author_match);
            }
            preg_match('/<http>(.*?)<\/http>/', $val, $http_match);
            if (!empty($http_match)) {
                $lists_result[$tkey][$key]['http'] = end($http_match);
            }
            preg_match('/<link>(.*?)<\/link>/', $val, $link_match);
            if (!empty($link_match)) {
                $lists_result[$tkey][$key]['link'] = strtolower(end($link_match));
            }

            preg_match('/<data>(.*?)<\/data>/', $val, $data_match);
            if (!empty($data_match)) {
                $data_rule = explode(',', str_replace('######', '', end($data_match)));
                foreach ($data_rule as $dkey => $dval) {
                    $field = explode('|', trim($dval));
                    $lists_result[$tkey][$key]["data"][trim($field[0])]['label'] = trim($field[1]);
                    if (isset($field[2])) {
                        $lists_result[$tkey][$key]["data"][trim($field[0])]['isrequire'] = trim($field[2]);
                    }
                    if (isset($field[3])) {
                        $lists_result[$tkey][$key]["data"][trim($field[0])]['type'] = trim($field[3]);
                    }
                }
            }
            preg_match('/<return>(.*?)<\/return>/', $val, $return_match);
            if(!empty($return_match)){
                //                 $lists_result[$tkey][$key]["return"] = strtolower(str_replace('######',"\n",end($return_match)));
                //                 $lists_result[$tkey][$key]["return"] = strtolower(str_replace("\"","&quot;",$lists_result[$tkey][$key]["return"]));
                $data_rule = explode(',', str_replace('######', '', end($return_match)));
                foreach ($data_rule as $dkey => $dval) {
                    $field = explode('|', trim($dval));
                    if (isset($field[1])) {
                        $lists_result[$tkey][$key]["return"][trim($field[0])]['desc'] = trim($field[1]);
                    }
                    if (isset($field[2])) {
                        $lists_result[$tkey][$key]["return"][trim($field[0])]['type'] = trim($field[2]);
                    }
                }
            }
        }
    }
    return $lists_result;
}

function display()
{
    $result = show_service_lists();
    //     print_r($result);die;
    $html = "<!DOCTYPE html><html><head><meta charset=\"utf-8\">
            <link rel='stylesheet' href='document.css' type='text/css'>
            <title>接口文档</title></head><body><article class=\"markdown-body\"><h1 id=\"v23\"><a name=\"user-content-v23\" href=\"#v23\" class=\"headeranchor-link\" aria-hidden=\"true\"><span class=\"headeranchor\"></span></a>接口文档</h1>
            <div>
            <ul>";
    foreach($result as $key => $val){
        $html .= "<li>
                    <a href=\"#{$key}\">{$key}</a >
                </li >";
    }
    $html .= "</ul>
            </div>";
    foreach ($result as $tkey => $tval) {
        $html .= "<h3 id=\"{$tkey}\"><a name=\"user-content-_{$tkey}\" href=\"#{$tkey}\" class=\"headeranchor-link\" aria-hidden=\"true\"><span class=\"headeranchor\"></span></a>{$tkey}</h3>";
        $html .= "<div><ul>";
        foreach($tval as $kkey => $vval){
            $html .="<li><a href=\"#{$vval['link']}\">{$vval['name']}</a></li>";
        }
        $html .="</ul></div>";
        foreach ($tval as $key => $val) {
            $val['http'] = isset($val['http'])?$val['http']:'POST';
            $html .= "<h4 id=\"{$val['link']}\"><a name=\"user-content-{$val['link']}\" href=\"#{$val['link']}\" class=\"headeranchor-link\" aria-hidden=\"true\"><span class=\"headeranchor\"></span></a>{$val['name']}</h4>";
            $html .= "<blockquote>
                    BusinessCode: {$val['code']}<br />
                    Author: {$val['author']}<br />
                    HttpMethod: <code>{$val['http']}</code><br />
                    Url: {$val['link']}</p>
                    </blockquote>";
            $html .= "<p>Request: </p>
                    <table>
                    <thead>
                    <tr>
                    <th>param</th>
                    <th>type</th>
                    <th>IsRequire</th>
                    <th>description</th>
                    </tr>
                    </thead>
                    <tbody>";
            foreach ($val['data'] as $dkey => $dval) {
                $html .= "<tr>
                    <td>{$dkey}</td>
                    <td>{$dval['type']}</td>
                    <td>{$dval['isrequire']}</td>
                    <td>{$dval['label']}</td>
                    </tr>";
            }
            $html .= '</tbody></table><br />';
            if(isset($val['return'])){
                $html .= "<p>Response:</p>
                           <table>
		                    <thead>
		                    <tr>
		                    <th>param</th>
		                    <th>description</th>
                            <th>type</th>
		                    </tr>
		                    </thead>
		                    <tbody>";
                foreach ($val['return'] as $dkey => $dval) {
                    $html .= "<tr>
	                    <td>{$dkey}</td>
	                    <td>{$dval['desc']}</td>
	                    <td>{$dval['type']}</td>
	                    </tr>";
                }
                $html .= '</tbody></table><br />';
            }
            else{
                $html .='<hr />';
            }
        }
    }

    echo $html;
}

display();